//
// Created by xuan on 2024/5/19.
//

#include "UPRE_BSP_UART_R1_F429.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"

uint8_t PathTo_rec[3] = {0};

uint8_t Path_num = 0;

static uint8_t Path_to_Location = 0;
/////TODO:手柄能够使用后该数组清空！！！！！！
bool Ball[12] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};

static union Switch_UINT8_T2Float_ {
    uint8_t P[4];
    float value;
} Switch_UINT8_T2Float;

static uint8_t Rec;

#define RXBUFFERSIZE  256     //最大接收字节数
uint8_t RxBuffer[RXBUFFERSIZE]; //接收数据
uint8_t Uart7_Rx_Cnt = 0; //接收缓冲计数


void MasterSlave_communication_Init(void) {
    HAL_UART_Receive_IT(&huart7, PathTo_rec, 3);
}

void MasterSlave_communication_Receive(void) {
    if (PathTo_rec[0] == 0x88 && PathTo_rec[2] == 0x11) {
        Path_num = PathTo_rec[1];
        printf("%d\r\n", Path_num);
    }
    if (PathTo_rec[0] == 0x33 && PathTo_rec[2] == 0x03) {
        if ((PathTo_rec[1] & 0x01)) {
            /**底盘主板球颜色信息发送成功********/
            /**显示方法 begin**/
            printf("ball color transmit OK!!\r\n");
            /**显示方法 end**/
        } else {
            /**底盘主板球颜色信息发送失败********/
            /**显示方法 begin**/
            printf("ball color transmit ERROR!!\r\n");
            /**显示方法 end**/
        }
    }
    // for (int i = 0; i < 3; ++i) {
    //     printf("%x\r\n",PathTo_rec[i]);
    // }
    HAL_UART_Receive_IT(&huart7, PathTo_rec, 3);
}

void MasterSlave_communication_Transmit_PathContinue(bool pathContinue_flag) {
    uint8_t PathFeedback[4] = {0};
    PathFeedback[0] = 0x11;
    PathFeedback[1] = pathContinue_flag;
    PathFeedback[2] = 0x00; ////TODO:该位用于数据包对齐，勿删
    PathFeedback[2] = 0x01;
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
    HAL_UART_Transmit(&huart7, (unsigned char *) &PathFeedback, sizeof PathFeedback, 0xffff);
    while (HAL_UART_GetState(&huart7) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
}

void MasterSlave_communication_Transmit_BallColor() {
    uint8_t Ball_tx[4] = {0};
    Ball_tx[0] = 0x22;
    Ball_tx[1] = Ball[0] | (Ball[1] << 1) | (Ball[2] << 2) | (Ball[3] << 3) | (Ball[4] << 4) | (Ball[5] << 5) |
                 (Ball[6] << 6) | (Ball[7] << 7);
    Ball_tx[2] = Ball[8] | (Ball[9] << 1) | (Ball[10] << 2) | (Ball[11] << 3) | (!Ball[8] << 4) | (!Ball[9] << 5) |
                 (!Ball[10] << 6) | (!Ball[11] << 7);
    Ball_tx[3] = 0x02;
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
    HAL_UART_Transmit(&huart7, Ball_tx, sizeof Ball_tx, 0xffff);
    while (HAL_UART_GetState(&huart7) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
}

void ManualModeState_Transmit(bool manualstate) {
    uint8_t ManualMode_tx[3] = {0};

    ManualMode_tx[0] = 0x44;
    ManualMode_tx[1] = manualstate & 0x01;
    ManualMode_tx[2] = 0x04;

    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
    HAL_UART_Transmit(&huart7, ManualMode_tx, sizeof ManualMode_tx, 0xffff);
    while (HAL_UART_GetState(&huart7) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
}

void ManualMode_Transmit(float dir, float size) {
    uint8_t ManualMode_tx[10] = {0};

    ManualMode_tx[0] = 0xc0;

    Switch_UINT8_T2Float.value = dir;

    ManualMode_tx[1] = Switch_UINT8_T2Float.P[0];
    ManualMode_tx[2] = Switch_UINT8_T2Float.P[1];
    ManualMode_tx[3] = Switch_UINT8_T2Float.P[2];
    ManualMode_tx[4] = Switch_UINT8_T2Float.P[3];

    Switch_UINT8_T2Float.value = size;

    ManualMode_tx[5] = Switch_UINT8_T2Float.P[0];
    ManualMode_tx[6] = Switch_UINT8_T2Float.P[1];
    ManualMode_tx[7] = Switch_UINT8_T2Float.P[2];
    ManualMode_tx[8] = Switch_UINT8_T2Float.P[3];

    ManualMode_tx[9] = 0x0c;

    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
    HAL_UART_Transmit(&huart7, ManualMode_tx, sizeof ManualMode_tx, 0xffff);
    while (HAL_UART_GetState(&huart7) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
}

extern void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
    if (huart == &huart7) {
        MasterSlave_communication_Receive();
        HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_9);
    }
}

void Communicate_Init(void) {
    HAL_UART_Receive_IT(&huart7, &Rec, 1);
}

void Communicate_Receive(void) {
    if (Uart7_Rx_Cnt >= 255) //溢出判断
    {
        Uart7_Rx_Cnt = 0;
        memset(RxBuffer, 0x00, sizeof(RxBuffer));
        HAL_UART_Transmit(&huart5, (uint8_t *) "data_error\r\n", 12, 0xFFFF);
    } else {
        RxBuffer[Uart7_Rx_Cnt++] = Rec; //接收数据转存

        if ((RxBuffer[Uart7_Rx_Cnt - 1] == 0x0A) && (RxBuffer[Uart7_Rx_Cnt - 2] == 0x0D)) //判断结束位
        {
            HAL_UART_Transmit(&huart5, (uint8_t *) &RxBuffer, Uart7_Rx_Cnt, 0xFFFF); //将收到的信息发送出去
            while (HAL_UART_GetState(&huart5) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
            Uart7_Rx_Cnt = 0;
            memset(RxBuffer, 0x00, sizeof(RxBuffer)); //清空数组
        }

        if ((RxBuffer[Uart7_Rx_Cnt - 1] == 0x11)) //判断结束位
        {
            Path_num = RxBuffer[1];
            printf("%d\r\n", Path_num);

            while (HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
            Uart7_Rx_Cnt = 0;
            memset(RxBuffer, 0x00, sizeof(RxBuffer)); //清空数组
        }

        if (RxBuffer[Uart7_Rx_Cnt - 1] == 0x03) {
            if (RxBuffer[1] & 0x01) {
                /**底盘主板球颜色信息发送成功********/
                /**显示方法 begin**/
                printf("ball color transmit OK!!\r\n");
                /**显示方法 end**/
            } else {
                /**底盘主板球颜色信息发送失败********/
                /**显示方法 begin**/
                printf("ball color transmit ERROR!!\r\n");
                /**显示方法 end**/
            }
            while (HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX); //检测UART发送结束
            Uart7_Rx_Cnt = 0;
            memset(RxBuffer, 0x00, sizeof(RxBuffer)); //清空数组
        }
    }

    Communicate_Init();
}
